package gen;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jdk.nashorn.internal.runtime.regexp.joni.Regex;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GenPack {
    public static final String BATH = System.getProperty("user.dir");

    public static void getQuestionsList(WebDriver driver) {
        try (FileWriter fw = new FileWriter("question_list.txt")) {


            driver.get("https://leetcode-cn.com/problemset/all/");
            ObjectMapper objectMapper = new ObjectMapper();
            while (true) {
                try {
                    WebElement element = driver.findElement(By.className("row-selector"));
                    element = element.findElement(By.className("form-control"));
                    Select select = new Select(element);
                    select.selectByVisibleText("全部");

                    break;
                } catch (Exception e) {
                    e.printStackTrace();
                    Thread.sleep(1000);
                }
            }

            WebElement element = driver.findElement(By.className("reactable-data"));
            List<WebElement> trElements = element.findElements(By.tagName("tr"));
            for (WebElement trElement : trElements) {
                List<WebElement> tds = trElement.findElements(By.tagName("td"));
                try {
                    WebElement i = tds.get(0).findElement(By.className("lock__13du"));
                } catch (Exception e) {

                    String id = tds.get(1).getText();
                    String name = null;
                    String url = null;
                    String solutionNum = null;
                    String solutionUrl = null;
                    String percentOfPass = "";
                    String levelMedium = null;

                    WebElement a = tds.get(2).findElement(By.tagName("a"));
                    url = a.getAttribute("href");
                    name = a.getText();
                    WebElement a1 = tds.get(3).findElement(By.tagName("a"));
                    solutionUrl = a1.getAttribute("href");
                    solutionNum = a1.getText();
                    percentOfPass = tds.get(4).getText();
                    levelMedium = tds.get(5).getText();

                    Question question = new Question();
                    question.setId(id);
                    question.setUrl(url);
                    question.setName(name);
                    question.setLevelMedium(levelMedium);
                    question.setSolutionUrl(solutionUrl);
                    question.setSolutionNum(solutionNum);
                    question.setPercentOfPass(percentOfPass);
                    System.out.println(question);
                    fw.write(objectMapper.writeValueAsString(question) + "\n");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void waitConfig() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("wait config success");
        String next = scanner.next();
        scanner.close();
        System.out.println(next);
    }

    public static void getFromLine(String base, String line) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            Question question = objectMapper.readValue(line, Question.class);
            String text = "";
            question.setContext(text);
            File file = new File(base + question.getFolderName());
            if (!file.exists()) {
                file.mkdir();
            }
            FileWriter fileWriter = new FileWriter(base + question.getFolderName() + "/" + "Solution" + ".java");
            fileWriter.write(question.toString());
            fileWriter.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void getFromFile(String file) {
        String base = BATH + "/src/main/java/ans/";
        try (BufferedReader bf = new BufferedReader(new FileReader(file))) {
            String line = null;
            while ((line = bf.readLine()) != null) {
                getFromLine(base, line);
            }

        } catch (Exception e) {

        }
    }

    public static void main(String[] args) throws Exception {
//        System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
//        WebDriver driver = new ChromeDriver();

        getFromFile(BATH + "/question_list.txt");
        //  driver.quit();


    }
}
